<template>
  <div v-if="visible" class="loading-overlay" :class="{ 'full-screen': fullScreen }">
    <div class="loading-content">
      <el-progress
        v-if="showProgress"
        :percentage="progress"
        :status="progressStatus"
        :stroke-width="8"
        class="progress-bar"
      />
      <div class="loading-spinner">
        <el-icon class="is-loading" :size="32">
          <Loading />
        </el-icon>
      </div>
      <div class="loading-text">
        {{ message }}
      </div>
      <div v-if="subMessage" class="loading-sub-text">
        {{ subMessage }}
      </div>
      <el-button
        v-if="cancellable && onCancel"
        type="danger"
        plain
        size="small"
        @click="handleCancel"
        class="cancel-button"
      >
        取消操作
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Loading } from '@element-plus/icons-vue'

interface Props {
  visible: boolean
  message?: string
  subMessage?: string
  progress?: number
  showProgress?: boolean
  progressStatus?: 'success' | 'exception' | 'warning' | ''
  fullScreen?: boolean
  cancellable?: boolean
  onCancel?: () => void
}

const props = withDefaults(defineProps<Props>(), {
  message: '加载中...',
  progress: 0,
  showProgress: false,
  progressStatus: '',
  fullScreen: false,
  cancellable: false
})

const handleCancel = () => {
  if (props.onCancel) {
    props.onCancel()
  }
}
</script>

<style scoped>
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.loading-overlay.full-screen {
  position: fixed;
  z-index: 2000;
}

.loading-content {
  text-align: center;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 200px;
}

.progress-bar {
  margin-bottom: 20px;
}

.loading-spinner {
  margin-bottom: 16px;
}

.loading-text {
  font-size: 16px;
  color: #606266;
  margin-bottom: 8px;
}

.loading-sub-text {
  font-size: 14px;
  color: #909399;
  margin-bottom: 16px;
}

.cancel-button {
  margin-top: 16px;
}

.is-loading {
  animation: rotating 2s linear infinite;
}

@keyframes rotating {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 暗色主题支持 */
.dark .loading-overlay {
  background-color: rgba(0, 0, 0, 0.8);
}

.dark .loading-content {
  background: var(--el-bg-color);
  color: var(--el-text-color-primary);
}
</style>